package com.stylefeng.guns.modular.api.controller;

import java.util.Date;

import javax.swing.text.html.parser.Entity;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.stylefeng.guns.core.base.controller.BaseController;
import com.stylefeng.guns.core.log.LogObjectHolder;
import com.stylefeng.guns.modular.api.service.IActivityService;
import com.stylefeng.guns.modular.platform.model.Activity;
import com.stylefeng.guns.modular.platform.model.BuyerDemand;

/**
 * 控制器
 *
 * @author fengshuonan
 * @Date 2018-07-25 20:00:45
 */
@Controller
@RequestMapping("/activity")
public class ActivityController extends BaseController {

    private String PREFIX = "/api/activity/";

    @Autowired
    private IActivityService activityService;

    /**
     * 跳转到首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "activity.html";
    }

    /**
     * 跳转到添加
     */
    @RequestMapping("/activity_add")
    public String activityAdd() {
        return PREFIX + "activity_add.html";
    }

    /**
     * 跳转到修改
     */
    @RequestMapping("/activity_update/{activityId}")
    public String activityUpdate(@PathVariable Integer activityId, Model model) {
        Activity activity = activityService.selectById(activityId);
        model.addAttribute("item",activity);
        LogObjectHolder.me().set(activity);
        return PREFIX + "activity_edit.html";
    }

    /**
     * 获取列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(Activity activity) {
    	EntityWrapper<Activity> wp =new EntityWrapper<Activity>();
    	wp.eq("app_id", activity.getAppId());
    	if(StringUtils.isNoneBlank(activity.getActivityName())){
    		wp.like("activity_name", activity.getActivityName());
    	}
        return activityService.selectList(wp);
    }

    /**
     * 新增
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(Activity activity) {
    	
    	activity.setStatus(1);
    	activity.setCreateId("1");
    	activity.setCreateTime(new Date());
    	activity.setUpdateTime(new Date());
        activityService.insert(activity);
        return SUCCESS_TIP;
    }

    /**
     * 删除
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Integer activityId) {
        activityService.deleteById(activityId);
        return SUCCESS_TIP;
    }

    /**
     * 修改
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(Activity activity) {
    	activity.setUpdateTime(new Date());
        activityService.updateById(activity);
        return SUCCESS_TIP;
    }

    /**
     * 详情
     */
    @RequestMapping(value = "/detail/{activityId}")
    @ResponseBody
    public Object detail(@PathVariable("activityId") Integer activityId) {
        return activityService.selectById(activityId);
    }
}
